package com.ityun.ogapp.wechat

import android.content.ComponentName
import android.content.Intent
import android.graphics.Rect
import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.ToastUtils
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.click
import com.ven.assists.AssistsCore.getBoundsInScreen
import com.ven.assists.service.AssistsService
import com.ven.assists.simple.step.StepTag
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import com.ven.assists.stepper.StepManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

object WechatManager {

    private var userName = ""


    // 打开微信视频
    fun openWechatVideo(userName: String) {
        this.userName = userName
        StepManager.execute(VideoStep::class.java.name, 1, begin = true)
    }


    fun stopStep() {
        StepManager.isStop = true
    }


    class VideoStep : StepImpl() {
        override fun onImpl(collector: StepCollector) {
            collector.next(StepTag.STEP_1) {
                // 启动微信到首页
                Intent().apply {
                    addCategory(Intent.CATEGORY_LAUNCHER)
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                    AssistsService.instance?.startActivity(this)
                }
                // 直接跳转到查找步骤，跳过通讯录
                return@next Step.get(StepTag.STEP_2, delay = 500)
            }.next(StepTag.STEP_2) {
                // 查找对应的用户
                val infos =
                    AssistsCore.findById("com.tencent.mm:id/kbq", filterText = userName)
                if (infos.isNotEmpty()) {
                    var clicked = false
                    infos.forEach { it1 ->
                        var parent: AccessibilityNodeInfo? = it1.parent
                        while (parent != null) {
                            if (parent.isClickable) {
                                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                                parent.recycle()
                                clicked = true
                                break
                            }
                            val tempParent = parent.parent
                            parent.recycle()
                            parent = tempParent
                        }
                    }
                    if (clicked) {
                        delay(1000) // 等待聊天界面加载
                        return@next Step.get(StepTag.STEP_3)
                    } else {
                        return@next Step.repeat
                    }
                } else {
                    return@next Step.repeat
                }
            }.next(StepTag.STEP_3) {
                // 查找更多功能的按钮
                val allNodes = AssistsCore.getAllNodes()
                if (allNodes.isNotEmpty()) {
                    allNodes.forEach { it ->
                        it.contentDescription?.let { desc ->
                            if (desc.isNotEmpty() && desc.contains("更多功能按钮")) {
                                it.click()
                                return@next Step.get(StepTag.STEP_4, delay = 500)
                            }
                        }
                    }
                }
                return@next Step.repeat
            }.next(StepTag.STEP_4) {
                val notes = AssistsCore.findById("com.tencent.mm:id/a12", filterText = "视频通话")
                if (notes.isNotEmpty()) {
                    notes.forEach {
                        val react = it.parent.getBoundsInScreen()
                        AssistsCore.gestureClick(react.left.toFloat()+10,react.top.toFloat()+10)
                        return@next Step.get(StepTag.STEP_5, delay = 500)
                    }
                }
                return@next Step.repeat
            }.next(StepTag.STEP_5){
                val notes = AssistsCore.findById("com.tencent.mm:id/obc", filterText = "视频通话")
                if (notes.isNotEmpty()) {
                    notes.forEach {
                        val react = it.getBoundsInScreen()
                        AssistsCore.gestureClick(react.left.toFloat(),react.top.toFloat())
                        return@next Step.get(StepTag.STEP_6, delay = 500)
                    }
                }

                return@next Step.repeat
            }
        }
    }

}
